﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace VWise_DAO
{
  public class CVehicleManager
  {
    public const int W_OK = 0;
    public const int W_FAIL = -1;

    public const int W_MARK_FAIL = -1;
    public const int W_MARK_INGRESO = 1;
    public const int W_MARK_SALIDA = 2;
    public const int W_MARK_EXCEPCIONAL_INGRESO = 3;
    public const int W_MARK_EXCEPCIONAL_SALIDA = 4;

    //cadena de conexion
    public string szConnectionString = "";

    //informacion de los sp's a ejecutar METODO GetVehicleByID
    public string szGetInfVehiculoSP = "SP_GET_INF_VEHICULO";
    public string szParamGetInfVehiculoSP = "@N_COD_VEHICULO";


    //informacion de los sp's a ejecutar METODO SearchMarks
    public string szGetRecMarVehicleSP = "SP_GET_RECORD_MARCAS_VEHICULOS";
    public string szParamGetRecMarVehicleSP01 = "@N_COD_VEHICULO";
    public string szParamGetRecMarVehicleSP02 = "@F_FECHA_INICIAL";
    public string szParamGetRecMarVehicleSP03 = "@F_FECHA_FINAL";

    //informacion de los sp's a ejecutar METODO RegisterVehicleMark
    public string szStIngVehiclelSP = "SP_SET_ING_VEHICULO";
    public string szParamStIngVehiclelSP01 = "@N_COD_VEHICULO";
    public string szParamStIngVehiclelSP02 = "@F_FECHA_MARCA";
    public string szParamStIngVehiclelSP03 = "@B_FLAG_ING_SAL";
    public string szParamStIngVehiclelSP04 = "@C_PTA_ACCESO";

    //informacion de los sp's a ejecutar METODO SearchVehicle
    public string szSearchVehicleSP = "SP_BUS_VEHICULO";
    public string szParamSearchVehicleSP01 = "@C_COD_PLACA";
    public string szParamSearchVehicleSP02 = "@C_PRI_NOMBRE";
    public string szParamSearchVehicleSP03 = "@C_SEG_NOMBRE";
    public string szParamSearchVehicleSP04 = "@C_APE_PATERNO";
    public string szParamSearchVehicleSP05 = "@C_APE_MATERNO";
    public string szParamSearchVehicleSP06 = "@C_NOM_PROPIETARIO";
    public string szParamSearchVehicleSP07 = "@C_MARCA";
    public string szParamSearchVehicleSP08 = "@V_MODELO";


    //informacion del SP CreateNewVehicle
    public string szCreateVehicleSP = "SP_NEW_VEHICLE";
    public string szParamCreateVehicleSP01 = "@C_COD_PLACA";
    public string szParamCreateVehicleSP02 = "@C_TIP_VEHICULO";
    public string szParamCreateVehicleSP03 = "@C_MAR_VEHICULO";
    public string szParamCreateVehicleSP04 = "@C_MOD_VEHICULO";
    public string szParamCreateVehicleSP05 = "@C_NOM_PROPIETARIO";
    public string szParamCreateVehicleSP06 = "@N_COD_PERSONA";

    //informacion del SP GetAllVehicles
    public string szGetAllVehicleSP = "SP_GET_ALLVEHICLES";

    //informacion del SP DeleteVehicles
    public string szDeleteVehicleSP = "SP_DELETE_VEHICLE";
    public string szParamDeleteVehicleSP01 = "@N_COD_VEHICULO";

    //informacion del SP CreateNewVehicle
    public string szUpdateVehicleSP = "SP_UPDATE_VEHICLE";
    public string szParamUpdateVehicleSP01 = "@N_COD_VEHICLE";
    public string szParamUpdateVehicleSP02 = "@C_COD_PLACA";
    public string szParamUpdateVehicleSP03 = "@C_TIP_VEHICULO";
    public string szParamUpdateVehicleSP04 = "@C_MAR_VEHICULO";
    public string szParamUpdateVehicleSP05 = "@C_MOD_VEHICULO";
    public string szParamUpdateVehicleSP06 = "@C_NOM_PROPIETARIO";
    public string szParamUpdateVehicleSP07 = "@N_COD_PERSONA";
      
    CDBAccess pDBAccess = new CDBAccess();

    public void Configure()
    {
      CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVehicleManager->Configure");

      try
      {
        //cadena de conexion
        szConnectionString = ConfigurationSettings.AppSettings["szConnectionString"];

        //informacion de los sp's a ejecutar METODO GetVehicleByID
        szGetInfVehiculoSP = ConfigurationSettings.AppSettings["szGetInfVehiculoSP"];
        szParamGetInfVehiculoSP = ConfigurationSettings.AppSettings["szParamGetInfVehiculoSP"];


        //informacion de los sp's a ejecutar METODO SearchMarks
        szGetRecMarVehicleSP = ConfigurationSettings.AppSettings["szGetRecMarVehicleSP"];
        szParamGetRecMarVehicleSP01 = ConfigurationSettings.AppSettings["szParamGetRecMarVehicleSP01"];
        szParamGetRecMarVehicleSP02 = ConfigurationSettings.AppSettings["szParamGetRecMarVehicleSP02"];
        szParamGetRecMarVehicleSP03 = ConfigurationSettings.AppSettings["szParamGetRecMarVehicleSP03"];

        //informacion de los sp's a ejecutar METODO RegisterVehicleMark
        szStIngVehiclelSP = ConfigurationSettings.AppSettings["szStIngVehiclelSP"];
        szParamStIngVehiclelSP01 = ConfigurationSettings.AppSettings["szParamStIngVehiclelSP01"];
        szParamStIngVehiclelSP02 = ConfigurationSettings.AppSettings["szParamStIngVehiclelSP02"];
        szParamStIngVehiclelSP03 = ConfigurationSettings.AppSettings["szParamStIngVehiclelSP03"];
        szParamStIngVehiclelSP04 = ConfigurationSettings.AppSettings["szParamStIngVehiclelSP04"];

        //informacion de los sp's a ejecutar METODO SearchVehicle
        szSearchVehicleSP = ConfigurationSettings.AppSettings["szSearchVehicleSP"];
        szParamSearchVehicleSP01 = ConfigurationSettings.AppSettings["szParamSearchVehicleSP01"];
        szParamSearchVehicleSP02 = ConfigurationSettings.AppSettings["szParamSearchVehicleSP02"];
        szParamSearchVehicleSP03 = ConfigurationSettings.AppSettings["szParamSearchVehicleSP03"];
        szParamSearchVehicleSP04 = ConfigurationSettings.AppSettings["szParamSearchVehicleSP04"];
        szParamSearchVehicleSP05 = ConfigurationSettings.AppSettings["szParamSearchVehicleSP05"];
        szParamSearchVehicleSP06 = ConfigurationSettings.AppSettings["szParamSearchVehicleSP06"];
        szParamSearchVehicleSP07 = ConfigurationSettings.AppSettings["szParamSearchVehicleSP07"];
        szParamSearchVehicleSP08 = ConfigurationSettings.AppSettings["szParamSearchVehicleSP08"];

        //informacion del SP CreateNewVehicle
        szCreateVehicleSP = ConfigurationSettings.AppSettings["szCreateVehicleSP"];
        szParamCreateVehicleSP01 = ConfigurationSettings.AppSettings["szParamCreateVehicleSP01"];
        szParamCreateVehicleSP02 = ConfigurationSettings.AppSettings["szParamCreateVehicleSP02"];
        szParamCreateVehicleSP03 = ConfigurationSettings.AppSettings["szParamCreateVehicleSP03"];
        szParamCreateVehicleSP04 = ConfigurationSettings.AppSettings["szParamCreateVehicleSP04"];
        szParamCreateVehicleSP05 = ConfigurationSettings.AppSettings["szParamCreateVehicleSP05"];
        szParamCreateVehicleSP06 = ConfigurationSettings.AppSettings["szParamCreateVehicleSP06"];

        //informacion del SP GetAllVehicles
        szGetAllVehicleSP = ConfigurationSettings.AppSettings["szGetAllVehicleSP"];

        //informacion del SP DeleteVehicles
        szDeleteVehicleSP = ConfigurationSettings.AppSettings["szDeleteVehicleSP"];
        szParamDeleteVehicleSP01 = ConfigurationSettings.AppSettings["szParamDeleteVehicleSP01"];

        //informacion del SP CreateNewVehicle
        szUpdateVehicleSP = ConfigurationSettings.AppSettings["szUpdateVehicleSP"];
        szParamUpdateVehicleSP01 = ConfigurationSettings.AppSettings["szParamUpdateVehicleSP01"];
        szParamUpdateVehicleSP02 = ConfigurationSettings.AppSettings["szParamUpdateVehicleSP02"];
        szParamUpdateVehicleSP03 = ConfigurationSettings.AppSettings["szParamUpdateVehicleSP03"];
        szParamUpdateVehicleSP04 = ConfigurationSettings.AppSettings["szParamUpdateVehicleSP04"];
        szParamUpdateVehicleSP05 = ConfigurationSettings.AppSettings["szParamUpdateVehicleSP05"];
        szParamUpdateVehicleSP06 = ConfigurationSettings.AppSettings["szParamUpdateVehicleSP06"];
        szParamUpdateVehicleSP07 = ConfigurationSettings.AppSettings["szParamUpdateVehicleSP07"];
      }
      catch (Exception ex)
      {
        //error en configuracion
        CLogProcessor.LogInfo(CLogProcessor.W_LOG_ERROR,"CVehicleManager->Configure: " + ex.ToString());
      }
    }

    public CVehicle GetVehicleByID(int iCodeToFind)
    {
      CVehicle pVehicle = null;

      Dictionary<string, object> pDictionary = null;
      SqlDataReader pReader = null;
      int iresult;

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG,"CVehicleManager->GetVehicleByID:" + iCodeToFind.ToString());

      //consulta a BD
      pDBAccess.szConectionString = this.szConnectionString;
      if (!pDBAccess.Connect())
      {
        return null;
      }

      pDictionary = new Dictionary<string, object>();
      pDictionary.Add(szParamGetInfVehiculoSP, iCodeToFind);

      iresult = pDBAccess.LaunchSQLStoreProcedure(szGetInfVehiculoSP, pDictionary, out pReader);
      if (iresult != W_OK || pReader == null)
      {
        pDBAccess.Disconnect();
        return null;
      }

      if (!pReader.HasRows)
      {
        pDBAccess.Disconnect();
        return null;
      }

      //match resultset con TOF CVehicle
      pVehicle = new CVehicle();

      //solo 1 fila ya que el PKCODE es unico
      if (!pReader.Read())
      {
        pReader.Close();
        pDBAccess.Disconnect();
        return null;
      }

      try
      {
        pVehicle.iPKCode = iCodeToFind;
        pVehicle.szPlaca = pReader.GetString(0);
        pVehicle.szTipoVehiculo =  pReader.GetString(1);
        pVehicle.szMarca = pReader.GetString(2); 
        pVehicle.szModelo = pReader.GetString(3);
        pVehicle.szPropietario = pReader.GetString(4);
        pVehicle.iPKCodeConductor = pReader.GetInt32(5);     //default no condition
        pVehicle.szConductor = pReader.GetString(6);
      }
      catch (Exception ex)
      {
        CLogProcessor.LogInfo(CLogProcessor.W_LOG_ERROR,"CVehicleManager->GetVehicleByID: " + ex.ToString());
        pVehicle = null;
      }

      pReader.Close();
      pDBAccess.Disconnect();
      
      return pVehicle;
    }

    public List<CVehicle> SearchForVehicle(string szName, string szPatLastName, string szMatLastName,
                                           string szPlaca, string szMarca, string szModelo)
    {
      List<CVehicle> lListVehicles = null;
      Dictionary<string, object> pDictionary = null;
      CVehicle pVehicle = null;
      SqlDataReader pReader = null;
      int iresult;

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG,"CVehicleManager->SearchForVehicle:" + szName + "," +
                                                                    szPatLastName + "," +
                                                                    szMatLastName + "," +
                                                                    szPlaca + "," +
                                                                    szMarca + "," +
                                                                    szModelo);
      //consulta a BD
      pDBAccess.szConectionString = this.szConnectionString;
      if (!pDBAccess.Connect())
      {
        return null;
      }

      pDictionary = new Dictionary<string, object>();
      pDictionary.Add(szParamSearchVehicleSP01, szPlaca);
      pDictionary.Add(szParamSearchVehicleSP02, szName);
      pDictionary.Add(szParamSearchVehicleSP03, "");
      pDictionary.Add(szParamSearchVehicleSP04, szPatLastName);
      pDictionary.Add(szParamSearchVehicleSP05, szMatLastName);
      pDictionary.Add(szParamSearchVehicleSP06, "");
      pDictionary.Add(szParamSearchVehicleSP07, szMarca);
      pDictionary.Add(szParamSearchVehicleSP08, szModelo);


      iresult = pDBAccess.LaunchSQLStoreProcedure(szSearchVehicleSP, pDictionary, out pReader);
      if (iresult != W_OK || pReader == null)
      {
        pDBAccess.Disconnect();
        return null;
      }

      if (!pReader.HasRows)
      {
        CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVehicleManager->SearchForVehicle: No results");
        pDBAccess.Disconnect();
        return null;
      }

      //match resultset con TOF CPersonal
      lListVehicles = new List<CVehicle>();

      while (pReader.Read())
      {
        pVehicle = new CVehicle();
        try
        {
          pVehicle.iPKCode = pReader.GetInt32(0);
          pVehicle.szPlaca = pReader.GetString(1);
          pVehicle.szTipoVehiculo = pReader.GetString(2);
          pVehicle.szMarca = pReader.GetString(3);
          pVehicle.szModelo = pReader.GetString(4);
          pVehicle.szPropietario = pReader.GetString(5);
          pVehicle.iPKCodeConductor = pReader.GetInt32(6);     //default no condition
          pVehicle.szConductor = pReader.GetString(7);
        }
        catch (Exception ex)
        {
          CLogProcessor.LogInfo(CLogProcessor.W_LOG_ERROR, "CVehicleManager->SearchForVehicle: " + ex.ToString());
          pVehicle = null;
        }

        if (pVehicle != null)
        {
          lListVehicles.Add(pVehicle);
        }
      }

      pReader.Close();
      pDBAccess.Disconnect();
      
      
      return lListVehicles;
    }

    public List<CMarkRegister> SearchMarks(CVehicle pVehicle, string szStartDate, string szEndDate)
    {
      if (pVehicle == null)
        return null;
      
      return SearchMarks(pVehicle.iPKCode, szStartDate, szEndDate);
    }

    public List<CMarkRegister> SearchMarks(int iPKCode, string szStartDate, string szEndDate)
    {
      List<CMarkRegister> lListMarks = null;
      Dictionary<string, object> pDictionary = null;
      SqlDataReader pReader = null;
      CMarkRegister pMark = null;
      int iresult;

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG,"CVehicleManager->SearchMarks:" + iPKCode.ToString() + "," +
                                                              szStartDate + "," +
                                                              szEndDate);

      pDBAccess.szConectionString = this.szConnectionString;
      if (!pDBAccess.Connect())
      {
        return null;
      }

      pDictionary = new Dictionary<string, object>();
      pDictionary.Add(szParamGetRecMarVehicleSP01, iPKCode);
      pDictionary.Add(szParamGetRecMarVehicleSP02, szStartDate);
      pDictionary.Add(szParamGetRecMarVehicleSP03, szEndDate);


      iresult = pDBAccess.LaunchSQLStoreProcedure(szGetRecMarVehicleSP, pDictionary, out pReader);
      if (iresult != W_OK || pReader == null)
      {
        pDBAccess.Disconnect();
        return null;
      }

      lListMarks = new List<CMarkRegister>();

      if (!pReader.HasRows)
      {
        pDBAccess.Disconnect();
        return lListMarks;
      }

      while (pReader.Read())
      {
        pMark = new CMarkRegister(CMarkRegister.VWISE_DHL_VEHICLE);

        try
        {
          pMark.iPKCode = iPKCode;
          DateTime.TryParse(pReader.GetString(1), out pMark.dtInOutTime);
          //pMark.iFlagRegister = pReader.GetInt32(2);
          pMark.szPuerta = pReader.GetString(2);
          pMark.codigoStr = pReader.GetString(3);
          pMark.area = pReader.GetString(4); //placa
          int estado = pReader.GetInt32(5);
          if (estado == 1) 
          {
              pMark.departamento = "ENTRADA";
          }
          else if(estado==2)
          {
              pMark.departamento = "SALIDA";  
          }
          else if (estado == 3)
          {
              pMark.departamento = "ENTRADA EXCEPCIONAL";
          }
          else if (estado == 4)
          {
              pMark.departamento = "SALIDA EXCEPCIONAL";
          }
          pMark.nombre = pReader.GetString(6);
          pMark.unidad = pReader.GetString(7);  

        }
        catch (Exception ex)
        {
          CLogProcessor.LogInfo(CLogProcessor.W_LOG_ERROR,"CVehiclelManager->SearchMarks: " + ex.ToString());
          pMark = null;
        }

        if (pMark != null)
        {
          lListMarks.Add(pMark);
        }
      }

      pReader.Close();
      pDBAccess.Disconnect();
      return lListMarks;

    }


    protected int CalculateEventMark(int iPKCode, List<CMarkRegister> lListMarks)
    {
      int iEventMark = W_MARK_INGRESO;
      CMarkRegister pMark = null;

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG,"CVehiclelManager->CalculateEventMark:" + iPKCode.ToString() +  "," +
                                                              lListMarks.Count.ToString());

      //TODO
      if(lListMarks == null)
      {
        //si hay error al obtener las marcas -> consideramos ingreso (REVISAR!!)
        return W_FAIL;
      }

      if (lListMarks.Count == 0)
      {
        //si el vehiculo no registra marcas se considera ingreso
        iEventMark = W_MARK_INGRESO;
      }
      else
      {
        //la lista de marcas de vehiculos para registro solo devuelve la ultima marca
        pMark = lListMarks[0];

        if (pMark == null)
        {
          //si hay error al obtener las marcas -> consideramos excepcional (REVISAR!!)
          return W_MARK_INGRESO;
        }

        switch (pMark.iFlagRegister)
        {
          case W_MARK_INGRESO:
            //ultimo fue ingreso -> salida
            iEventMark = W_MARK_SALIDA;
            break;
          case W_MARK_SALIDA:
            //ultimo fue salida -> ingreso
            iEventMark = W_MARK_INGRESO;
            break;
        }
      }

      return iEventMark;
    }

    public int RegisterVehicleMark(CVehicle pVehicle, DateTime dtRegTime, string szPuertaAcceso)
    {
      if (pVehicle == null)
        return W_FAIL;

      return RegisterVehicleMark(pVehicle.iPKCode, dtRegTime, szPuertaAcceso);
    }

    public int RegisterVehicleMark(int iPKCode, DateTime dtRegTime, string szPuertaAcceso)
    {
      //traer marcas de la persona/vehiculo
      List<CMarkRegister> lListMarks = null;
      Dictionary<string, object> pDictionary = null;
      int iEventmark;
      int iresult;
      int iRowsAffected = 0;

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG,"CVehiclelManager->RegisterVehicleMark:" + iPKCode.ToString() + "," +
                                                              dtRegTime.ToString("yyyy/MM/dd HH:mm:ss") + "," +
                                                              szPuertaAcceso);

      lListMarks = SearchMarks(iPKCode, "", "");

      if (lListMarks == null)
        return W_FAIL;

      //algoritmo de procesamiento de marcas, determinar si es entrada/salida/excepcional
      iEventmark = CalculateEventMark(iPKCode, lListMarks);

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO,"CVehiclelManager->RegisterVehicleMark: iEventmark = " + iEventmark.ToString());

      if (iEventmark == W_FAIL)
        return W_FAIL;

      //ingresar marca en la BD
      pDBAccess.szConectionString = this.szConnectionString;
      if (!pDBAccess.Connect())
      {
        return W_FAIL;
      }

      pDictionary = new Dictionary<string, object>();
      pDictionary.Add(szParamStIngVehiclelSP01, iPKCode);
      pDictionary.Add(szParamStIngVehiclelSP02, dtRegTime.ToString("yyyyMMdd HH:mm:ss"));
      pDictionary.Add(szParamStIngVehiclelSP03, iEventmark);
      pDictionary.Add(szParamStIngVehiclelSP04, szPuertaAcceso);

      iresult = pDBAccess.LaunchSQLStoreProcedure(szStIngVehiclelSP, pDictionary, out iRowsAffected);
      if (iresult != W_OK)
      {
        pDBAccess.Disconnect();
        return W_FAIL;
      }

      pDBAccess.Disconnect();

      return iEventmark;
    }

    public int CreateNewVehicle(CVehicle pVehicle)
    {
      Dictionary<string, object> pDictionary = null;
      int iresult;
      int iRowsAffected = 0;

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVehiclelManager->CreateNewVehicle");

      if (pVehicle == null)
        return W_FAIL;

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVehiclelManager->CreateNewVehicle: PLACA = " + pVehicle.szPlaca);

      //ingresar marca en la BD
      pDBAccess.szConectionString = this.szConnectionString;
      if (!pDBAccess.Connect())
      {
        return W_FAIL;
      }

      pDictionary = new Dictionary<string, object>();
      pDictionary.Add(szParamCreateVehicleSP01, pVehicle.szPlaca);
      pDictionary.Add(szParamCreateVehicleSP02, pVehicle.szTipoVehiculo);
      pDictionary.Add(szParamCreateVehicleSP03, pVehicle.szMarca);
      pDictionary.Add(szParamCreateVehicleSP04, pVehicle.szModelo);
      pDictionary.Add(szParamCreateVehicleSP05, pVehicle.szPropietario);
      pDictionary.Add(szParamCreateVehicleSP06, pVehicle.iPKCodeConductor);
      
      iresult = pDBAccess.LaunchSQLStoreProcedure(szCreateVehicleSP, pDictionary, out iRowsAffected);
      if (iresult != W_OK)
      {
        pDBAccess.Disconnect();
        return W_FAIL;
      }

      pDBAccess.Disconnect();

      return W_OK;
    }

    public List<CVehicle> GetAllVehicles()
    {
      List<CVehicle> lListVehicles = null;
      Dictionary<string, object> pDictionary = null;
      CVehicle pVehicle = null;
      SqlDataReader pReader = null;
      int iresult;

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVehiclelManager->GetAllVehicles");

      //consulta a BD
      pDBAccess.szConectionString = this.szConnectionString;
      if (!pDBAccess.Connect())
      {
        return null;
      }

      pDictionary = new Dictionary<string, object>();
      
      iresult = pDBAccess.LaunchSQLStoreProcedure(szGetAllVehicleSP, pDictionary, out pReader);
      if (iresult != W_OK || pReader == null)
      {
        pDBAccess.Disconnect();
        return null;
      }

      if (!pReader.HasRows)
      {
        CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVehicleManager->GetAllVehicles: No results");
        pDBAccess.Disconnect();
        return null;
      }

      //match resultset con TOF CPersonal
      lListVehicles = new List<CVehicle>();

      while (pReader.Read())
      {
        pVehicle = new CVehicle();
        try
        {
          pVehicle.iPKCode = pReader.GetInt32(0);
          pVehicle.szPlaca = pReader.GetString(1);
          pVehicle.szTipoVehiculo = pReader.GetString(2);
          pVehicle.szMarca = pReader.GetString(3);
          pVehicle.szModelo = pReader.GetString(4);
          pVehicle.szPropietario = pReader.GetString(5);
          pVehicle.iPKCodeConductor = pReader.GetInt32(6);     //default no condition
          pVehicle.szConductor = pReader.GetString(7);
        }
        catch (Exception ex)
        {
          CLogProcessor.LogInfo(CLogProcessor.W_LOG_ERROR, "CVehicleManager->GetAllVehicles: " + ex.ToString());
          pVehicle = null;
        }

        if (pVehicle != null)
        {
          lListVehicles.Add(pVehicle);
        }
      }

      pReader.Close();
      pDBAccess.Disconnect();


      return lListVehicles;
    }

    public int DeleteVehicle(CVehicle pVehicle)
    {
      Dictionary<string, object> pDictionary = null;
      int iresult;
      int iRowsAffected = 0;

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVehiclelManager->DeleteVehicle");

      if (pVehicle == null)
        return W_FAIL;

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVehiclelManager->DeleteVehicle: PLACA = " + pVehicle.szPlaca);

      //ingresar marca en la BD
      pDBAccess.szConectionString = this.szConnectionString;
      if (!pDBAccess.Connect())
      {
        return W_FAIL;
      }

      pDictionary = new Dictionary<string, object>();
      pDictionary.Add(szParamDeleteVehicleSP01, pVehicle.iPKCode);
      
      iresult = pDBAccess.LaunchSQLStoreProcedure(szDeleteVehicleSP, pDictionary, out iRowsAffected);
      if (iresult != W_OK)
      {
        pDBAccess.Disconnect();
        return W_FAIL;
      }

      pDBAccess.Disconnect();

      return W_OK;
    }

    public int UpdateVehicle(CVehicle pVehicle)
    {
      Dictionary<string, object> pDictionary = null;
      int iresult;
      int iRowsAffected = 0;

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_DEBUG, "CVehiclelManager->UpdateVehicle");

      if (pVehicle == null)
        return W_FAIL;

      CLogProcessor.LogInfo(CLogProcessor.W_LOG_INFO, "CVehiclelManager->UpdateVehicle: PLACA = " + pVehicle.szPlaca);

      //ingresar marca en la BD
      pDBAccess.szConectionString = this.szConnectionString;
      if (!pDBAccess.Connect())
      {
        return W_FAIL;
      }

      pDictionary = new Dictionary<string, object>();
      pDictionary.Add(szParamUpdateVehicleSP01, pVehicle.iPKCode);
      pDictionary.Add(szParamUpdateVehicleSP02, pVehicle.szPlaca);
      pDictionary.Add(szParamUpdateVehicleSP03, pVehicle.szTipoVehiculo);
      pDictionary.Add(szParamUpdateVehicleSP04, pVehicle.szMarca);
      pDictionary.Add(szParamUpdateVehicleSP05, pVehicle.szModelo);
      pDictionary.Add(szParamUpdateVehicleSP06, pVehicle.szPropietario);
      pDictionary.Add(szParamUpdateVehicleSP07, pVehicle.iPKCodeConductor);

      iresult = pDBAccess.LaunchSQLStoreProcedure(szUpdateVehicleSP, pDictionary, out iRowsAffected);
      if (iresult != W_OK)
      {
        pDBAccess.Disconnect();
        return W_FAIL;
      }

      pDBAccess.Disconnect();

      return W_OK;
    }
    
  }
}
